package com.ailianlian.ablecisi.repository;

import android.content.Context;

import com.ailianlian.ablecisi.baseclass.BaseRepository;
import com.ailianlian.ablecisi.constant.StatusCodeConstant;
import com.ailianlian.ablecisi.pojo.entity.AiCharacter;
import com.ailianlian.ablecisi.result.Result;
import com.ailianlian.ablecisi.utils.HttpClient;
import com.ailianlian.ablecisi.utils.JsonUtil;
import com.google.common.reflect.TypeToken;

import java.lang.reflect.Type;

/**
 * ailianlian
 * com.ailianlian.ablecisi.repository
 * CharacterRepository <br>
 *
 * @author Ablecisi
 * @version 1.0
 * 2025/6/16
 * 星期一
 * 20:14
 */
public class CharacterRepository extends BaseRepository {

    public CharacterRepository(Context context) {
        super(context);
    }

    public void loadCharacterById(String characterId, DataCallback<AiCharacter> dataCallback) {
        getExecutorService().execute(() -> {
            HttpClient.doGet(getContext(), "/character?characterId=" + characterId, new HttpClient.HttpCallback() {
                @Override
                public void onSuccess(String response) {
                    Type type = new TypeToken<Result<AiCharacter>>() {
                    }.getType();
                    Result<AiCharacter> result = JsonUtil.fromJson(response, type);
                    if (result != null && result.getCode() == StatusCodeConstant.SUCCESS) {
                        AiCharacter aiCharacter = result.getData();
                        if (aiCharacter != null) {
                            dataCallback.onSuccess(aiCharacter);
                        } else {
                            dataCallback.onError("角色信息为空");
                        }
                    } else {
                        dataCallback.onError(result != null ? result.getMsg() : "获取角色信息失败");
                    }
                }

                @Override
                public void onFailure(String error) {
                    dataCallback.onNetworkError();
                }
            });
        });
    }
}
